<template>
  <div id="dialogcontent">
    <el-row>
      <el-col :span="24">
        <div class="form-box">
          <el-form :model="invitationForm" ref="invitationForm" :rules="rules" label-position="left" label-width="80px">
            <el-form-item label="邀请类型" required>
              <el-select v-model="invitationForm.type" disabled class="form-width">
                <el-option
                  v-for="(item,index) in invitationList"
                  :key="index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="面试岗位" prop="job_id">
              <el-select v-model="invitationForm.job_id" class="form-width">
                <el-option
                  v-for="(item,index) in positionList"
                  :key="index"
                  :label="item.job_title"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="面试类型" prop="interview_type">
              <el-select v-model="invitationForm.interview_type" class="form-width">
                <el-option
                  v-for="(item,index) in types"
                  :key="index"
                  :label="item.label"
                  :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="面试时间" prop="interview_time">
              <el-date-picker v-model="invitationForm.interview_time" type="datetime" style="width:100%">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="面试地点" prop="address">
              <el-input v-model="invitationForm.interview_place" disabled
                        v-if="invitationForm.interview_type === 1" placeholder="无需填写"></el-input>
              <el-input v-model="invitationForm.interview_place" v-else></el-input>
            </el-form-item>
            <el-form-item label="面试要求" prop="requirements">
              <el-input v-model="invitationForm.other_requirements"></el-input>
            </el-form-item>
            <el-form-item style="margin-left:60px;">
              <el-button type="primary" v-if="type==='add'"
                         @click="submitForm('invitationForm')">发送
              </el-button>
              <el-button type="primary" v-if="type==='edit'"
                         @click="submitForm('invitationForm')">保存
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import { postDataToServer } from '../../assets/js/post.js'
  import { formatterTime } from '../../assets/js/common.js'
  import { getStaticMapList } from '../../assets/js/GlobeDefine.js'

  export default {
    name: 'dialogcontent',
    props: ['type', 'id'],
    data () {
      var validateDate = (rule, value, callback) => {
        if (this.invitationForm.interview_time < Date.now()) {
          callback(new Error('请选择有效的面试时间'))
        }
        callback()
      }
      return {
        invitationForm: {
          type: 0,
          job_id: '',
          interview_type: '',
          interview_time: '',
          interview_place: '',
          other_requirements: ''
        },
        invitationList: [{
          value: 0,
          label: '面试通知'
        }, {
          value: 1,
          label: '邀请函'
        }],
        types: [{
          value: 0,
          label: '现场面试'
        }, {
          value: 1,
          label: '视频面试'
        }, {
          value: 2,
          label: '电话面试'
        }],
        positionList: [],
        rules: {
          job_id: [
            {required: true, message: '请选择面试岗位', trigger: 'change'}
          ],
          interview_type: [
            {type: 'number', required: true, message: '请选择面试类型', trigger: 'change'}
          ],
          interview_time: [
            {type: 'date', validator: validateDate, trigger: 'change'}
          ],
          address: [
            {min: 0, max: 80, message: '长度在0到80个字', trigger: 'blur'}
          ],
          requirements: [
            {min: 0, max: 200, message: '长度在0到200个字', trigger: 'blur'}
          ]
        }
      }
    },
    created () {
      if (this.type === 'edit') {
        this.loadInvitationData(this.id)
      } else if (this.type === 'add') {
        this.invitationForm.resume_id = this.id
      }
      this.getComboboxData()  //加载该公司发布的岗位
    },
    methods: {
      submitForm (formName) {
        this.invitationForm.interview_time = formatterTime('date', this.invitationForm.interview_time)
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.type === 'add') {
              this.AddInvitation()
            } else if (this.type === 'edit') {
              this.EditInvitation()
            }
          } else {
            this.$message({
              type: 'info',
              message: '数据格式出错'
            })
            return false
          }
        })
      },
      AddInvitation () {  //发送邀请
        let _this = this
        postDataToServer('HM_COMPANY_INTERVIEW_ADD', _this.invitationForm)
          .then(function (res) {
            if (res.status === 0) {
              _this.$emit('stateChange', false)
              _this.$refs['invitationForm'].resetFields()//清空表单
              if ('data_list' in res) {
                _this.$message({
                  message: res.data_list[0],
                  type: 'warning'
                })
              } else {
                _this.$message({
                  message: '邀请成功，正在等待回复',
                  type: 'info'
                })
              }
            } else {
              _this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
      },
      EditInvitation () {  //编辑邀请
        let _this = this
        _this.invitationForm.id = _this.id
        postDataToServer('HM_COMPANY_INTERVIEW_EDIT', _this.invitationForm)
          .then(function (res) {
            if (res.status === 0) {
              _this.$emit('stateChange', false)
              _this.$message({
                message: '修改成功',
                type: 'info'
              })
            } else {
              _this.$message({
                message: res.message,
                type: 'error'
              })
            }
          })
      },
      loadInvitationData (id) {
        let _this = this
        let data = new Object()
        data.id = _this.id
        postDataToServer('HM_COMPANY_INTERVIEW_GET', data)
          .then(function (res) {
            if (res.status === 0) {
              for (let key in _this.invitationForm) {
                _this.invitationForm[key] = res.data_list[key]
              }
            }
          })
          .catch(function (err) {
            _this.$message({
              message: err,
              type: 'error'
            })
          })
      },
      getComboboxData () {
        let _this = this
        let data = new Object()
        data.page_no = 1
        data.page_size = 20
        data.company_id = _this.$store.state.companyID
        postDataToServer('HM_COMPANY_JOB_LISTPAGE', data)
          .then(function (res) {
            if (res.status === 0) {
              _this.positionList = res.data_list
            }
          })
      }
    },
    watch: {
      id: function (newValue, oldValue) {
        this.loadInvitationData(newValue)
        this.getComboboxData()
      }
    }
  }
</script>
<style scoped>
  .form-box {
    padding: 0px 10px;
  }

  .form-width {
    width: 100%;
  }
</style>
